import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { NestExpressApplication } from "@nestjs/platform-express";
import { setupSwagger } from "./setupSwagger";
import { AllExceptionsFilter } from "./common/filters/all-exception.filter";
import { ValidationPipe } from "@nestjs/common";
import * as history from "connect-history-api-fallback";

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);

  app.setGlobalPrefix("tinytimi"); // 全局路由前缀
  // 全局异常过滤器
  app.useGlobalFilters(new AllExceptionsFilter());

  // 全局参数校验管道
  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true, // 启用白名单，dto中没有声明的属性自动过滤
      transform: true, // 自动类型转换
    }),
  );

  /* 启动 vue 的 history模式 */
  // app.use(
  //   history({
  //     rewrites: [
  //       {
  //         from: /^\/swagger-ui\/.*$/,
  //         to: function (context) {
  //           return context.parsedUrl.pathname;
  //         },
  //       },
  //     ],
  //   }),
  // );

  // 启动swagger
  setupSwagger(app);

  await app.listen(3000);

  /* 打印swagger地址 */
  console.log("http://127.0.0.1:3000/swagger-ui/");
}
bootstrap();
